package com.sqbr.video.service;

import cn.hutool.core.lang.UUID;
import com.sqbr.video.cache.core.constants.CacheConstants;
import com.wf.captcha.SpecCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class CaptchaServiceImpl implements ICaptchaService {

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 生成图形验证码
     * @return key 验证码key  base64图片验证码
     */
    @Override
    public HashMap<String, String> defaultCaptChaImage() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为5分钟
        redisTemplate.opsForValue().set(CacheConstants.KUN_CACHE_NAME+"_IMAGE_CAPTCHA"+key, verCode, 500, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        HashMap<String, String> map = new HashMap<>();
        map.put("key", key);
        map.put("image", specCaptcha.toBase64());
        log.info("用户调用了验证码接口 验证码为{}",verCode);
        return map;
    }
}
